using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SypConst.Garbage;
using Tamir.SharpSsh;

namespace SypGui.ConnectionUC
{
    public partial class ScpStep2 : SypUCConnection
    {
        protected override void InitializeUC()
        {
            connMode = ConnectionMode.SCP_MODE;
        }

        public ScpStep2() : base ()
        {
        }

        public ScpStep2(SetText outputMethod)
            :base(outputMethod)
        {
        }


        protected override void InitializeRessources()
        {
            SCPStep2GroupBox.Text = GetRessources("WizardScpSettings");
            //ScpStep2HostnameText.Text = GetRessources("WizardScpHostname");
            //ScpStep2PasswordText.Text = GetRessources("WizardScpPassword");
            //ScpStep2UserText.Text = GetRessources("WizardScpUser");
            //ScpStep2RemotePathText.Text = GetRessources("WizardScpRemotePath");

            //ScpStep2HostnameText.Text = "stanhome";
            //ScpStep2PasswordText.Text = "stanhome";
            //ScpStep2UserText.Text = "root";

            ScpStep2HostnameText.Text = "88.191.224.28";
            ScpStep2PasswordText.Text = "dea0AiSi";
            ScpStep2UserText.Text = "root";
            ScpStep2PortText.Text = "22";

            ScpStep2RemotePathText.Text = "/var/www/syp";
            ScpStep2TestButton.Text = GetRessources("WizardTestButton");
        }

        public override void Reset()
        {
            base.Reset();
            //ScpStep2HostnameText.Text = GetRessources("WizardScpHostname");
            //ScpStep2PasswordText.Text = GetRessources("WizardScpPassword");
            //ScpStep2UserText.Text = GetRessources("WizardScpUser");
            //ScpStep2RemotePathText.Text = GetRessources("WizardScpRemotePath");
            //ScpStep2HostnameText.Text = "stanhome";
            //ScpStep2PasswordText.Text = "stanhome";
            //ScpStep2UserText.Text = "root";

            ScpStep2RemotePathText.Text = "/var/www/syp";
        }


        #region accessors

        public string ScpPasswordText
        {
            get
            {
                return ScpStep2PasswordText.Text;
            }
            set
            {
                ScpStep2PasswordText.Text = value;
            }
        }

        public string ScpRemotePathText
        {
            get
            {
                return ScpStep2RemotePathText.Text;
            }
            set
            {
                ScpStep2RemotePathText.Text = value;
            }
        }

        public string ScpUserText
        {
            get
            {
                return ScpStep2UserText.Text;
            }
            set
            {
                ScpStep2UserText.Text = value;
            }
        }

        public string ScpHostnameText
        {
            get
            {
                return ScpStep2HostnameText.Text;
            }
            set
            {
                ScpStep2HostnameText.Text = value;
            }
        }

        public int ScpPortText
        {
            get
            {
                int port = 22;
                return (int.TryParse(ScpStep2PortText.Text, out port))? port : 22;
            }
            set
            {
                ScpStep2PortText.Text = value.ToString();
            }
        }

        #endregion

        

        public override void ProcessConfig()
        {
            string cmd = "Setconnection-site" + SypGarbage.COMMAND_SEPARATOR + SiteId + SypGarbage.COMMAND_SEPARATOR + (ushort)connMode + SypGarbage.COMMAND_SEPARATOR + "\"" + ScpStep2HostnameText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + "\"" + ScpStep2PasswordText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + "\"" + ScpStep2UserText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + "\"" + ScpStep2RemotePathText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + ScpStep2PortText.Text;
            OutputMethod(cmd, Output.OUT);
            OutputMethod(cmd, Output.COMMAND);
            base.ProcessConfig();
        }

        #region events

        private void ScpStep2HostnameText_Enter(object sender, EventArgs e)
        {
            if (ScpStep2HostnameText.Text == GetRessources("WizardScpHostname"))
                ScpStep2HostnameText.Text = "";
        }


        private void ScpStep2PasswordText_Enter(object sender, EventArgs e)
        {
            if (ScpStep2PasswordText.Text == GetRessources("WizardScpPassword"))
                ScpStep2PasswordText.Text = "";
        }

        private void ScpStep2UserText_Enter(object sender, EventArgs e)
        {
            if (ScpStep2UserText.Text == GetRessources("WizardScpUser"))
                ScpStep2UserText.Text = "";
        }

        private void ScpStep2RemotePathText_Enter(object sender, EventArgs e)
        {
            if (ScpStep2RemotePathText.Text == GetRessources("WizardScpRemotePath"))
                ScpStep2RemotePathText.Text = "";
        }

        private void ScpStep2TestButton_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                SshStream mySshClient = new SshStream(ScpHostnameText, ScpUserText, ScpPasswordText, ScpPortText);

                mySshClient.Prompt = "#";
                mySshClient.RemoveTerminalEmulationCharacters = true;
                mySshClient.ReadResponse();

                mySshClient.Write("mkdir " + ScpRemotePathText + "");
                mySshClient.ReadResponse();
                mySshClient.Write("mkdir " + ScpRemotePathText + "/toto");
                mySshClient.ReadResponse();
                //mySshClient.Write("rm -rf  " + ScpRemotePathText + '/' + toto);
                mySshClient.Close();
                MessageBox.Show(GetRessources("ScpRemoteOK"));
            }
            catch (Exception exp)
            {
                MessageBox.Show(GetRessources("ScpRemoteKO") + " : " + exp.Message);
            }
        }

        #endregion

    }
}
